﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

using ESOL.Common.Entity;
using ESOL.Business.Chk;
using ESOL.Business.Prod;
using ESOL.Business.Mst;
using ESOL.Business.Sales;
using ESOL.Common;

namespace ESOL.WinUI.Qc
{
    public partial class frmMaintainIPQCApprove : BaseForm.frmBaseDialogyMaintainForm
    {
        #region Variable
        public ActionType ThisActionType { set; get; }
        private QcIpqcOrderEntity mSelectedIpqcOrder;
        #endregion

        #region Constructor
        public frmMaintainIPQCApprove()
        {
            InitializeComponent();
        }
        public frmMaintainIPQCApprove(QcIpqcOrderEntity entity)
        {
            InitializeComponent();
            mSelectedIpqcOrder = entity;
        }
        #endregion

        #region Function
        private void Init()
        {
            if (ThisActionType == ActionType.Approve)
            {
                btnSave.Enabled = true;
                btnConfirm.Enabled = false;
            }
            else if (ThisActionType == ActionType.Confirm)
            {
                btnSave.Enabled = false;
                btnConfirm.Enabled = true;
            }
        }
        private void BindCombobox()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("name");
            dt.Columns.Add("value", typeof(int));
            dt.Rows.Add("", -1);
            dt.Rows.Add("通过", 1);
            dt.Rows.Add("返修", 2);
            dt.Rows.Add("报废", 3);
            repositoryItemGridLookUpEdit1.DataSource = dt;
            repositoryItemGridLookUpEdit1.DisplayMember = "name";
            repositoryItemGridLookUpEdit1.ValueMember = "value";

            DataTable dt1 = new DataTable();
            dt1.Columns.Add("name");
            dt1.Columns.Add("value", typeof(int));
            dt1.Rows.Add("全检", 0);
            dt1.Rows.Add("抽检", 1);
            cmbCheckType.DataSource = dt1;
            cmbCheckType.DisplayMember = "name";
            cmbCheckType.ValueMember = "value";
        }
        private void SetField()
        {
            if (mSelectedIpqcOrder == null)
                return;
            ProdPlanOrderItemEntity mSelectedPlanOrderItem = ProdPlanOrderItemBs.GetProdPlanOrderItemByID(mSelectedIpqcOrder.ProdPlanOrderItemId);
            if (mSelectedPlanOrderItem == null)
                return;
            MstProductEntity mProduct = MstProductBs.GetMstProductTypeByID(mSelectedPlanOrderItem.ProductId);
            if (mProduct == null)
                return;
            ProdPlanOrderEntity mPlanOrder = ProdPlanOrderBs.GetProdPlanOrderByID(mSelectedPlanOrderItem.PlanOrderId);
            if (mPlanOrder == null)
                return;
            SalesProdPlanApplicationItemEntity mApplicationItem = SalesProdPlanApplicationItemBs.GetProdPlanApplicationItemByID(mPlanOrder.ApplicationItemId);
            if (mApplicationItem == null)
                return;
            txtIpqcOrder.Text = mSelectedIpqcOrder.OrderCode;
            txtWorkingOrder.Text = mSelectedPlanOrderItem.OrderItemCode;
            txtProductName.Text = mProduct.ProductName;
            txtProductCode.Text = mProduct.ProductCode;
            txtProductDescription.Text = mProduct.Description;
            txtContractModel.Text = mApplicationItem.ProductSpecOut;
            txtProductMaterial.Text = mProduct.Material;
            txtPlanQty.Text = mSelectedPlanOrderItem.Qty.ToString();
            txtCheckQty.Text = mSelectedIpqcOrder.CheckQty.ToString();
            txtNgQty.Text = mSelectedIpqcOrder.NgQty.ToString();
            cmbCheckType.SetSelectedValue(mSelectedIpqcOrder.CheckType);
        }
        private void BindGrid()
        {
            if (mSelectedIpqcOrder == null)
                return;
            string sCondition = " and qioi.ORDER_ID='" + mSelectedIpqcOrder.OrderId + "' ";
            DataTable dtNg = QcIpqcOrderItemBs.GetDtAllByCondition(sCondition);
            foreach (DataRow drNg in dtNg.Rows)
            {
                if (ThisActionType == ActionType.Approve)
                {
                    drNg["APPROVE_RESULT"] = -1;
                }
            }
            gvMain.DataSource = dtNg;
        }

        private bool CheckData()
        {
            DataTable dt = gvMain.DataSource as DataTable;
            if (dt.Rows.Count == 0)
            {
                MessageBox.Show("质检明细不能为空!", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            foreach (DataRow dr in dt.Rows)
            {
                if (Convert.ToInt32(dr["APPROVE_RESULT"]) == -1)
                {
                    MessageBox.Show("审核结果不能为空!", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return false;
                }
            }
            return true;
        }
        private bool Approve()
        {
            mSelectedIpqcOrder.Status = "STATUS03";
            DataTable dt = gvMain.DataSource as DataTable;
            List<QcIpqcOrderItemEntity> list = new List<QcIpqcOrderItemEntity>();
            foreach (DataRow dr in dt.Rows)
            {
                QcIpqcOrderItemEntity m = new QcIpqcOrderItemEntity(dr);
                list.Add(m);
            }
            if (QcIpqcOrderBs.ApproveNg(mSelectedIpqcOrder, list))
            {
                return true;
            }
            else
            {
                MessageBox.Show("保存失败!", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
        }
        #endregion

        private void frmMaintainIPQCApprove_Load(object sender, EventArgs e)
        {
            Init();
            BindCombobox();
            SetField();
            BindGrid();
        }
        protected override void Click_Save()
        {
            if (CheckData() == false)
                return;
            if (Approve())
            {
                MessageBox.Show("保存成功!", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.DialogResult = DialogResult.OK;
                return;
            }
        }

        private void btnConfirm_Click(object sender, EventArgs e)
        {
            if (mSelectedIpqcOrder == null)
                return;
            mSelectedIpqcOrder.Status = "STATUS06";
            if (QcIpqcOrderBs.UpdateQcIpqcOrder(mSelectedIpqcOrder))
            {
                MessageBox.Show("确认完成!", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.DialogResult = DialogResult.OK;
                return;
            }
        }



    }
}